package tianrun.ziguan.api.calculate.config.service.common.edit.asset;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.shiro.util.Assert;
import org.springframework.stereotype.Service;
import tianrun.ziguan.api.calculate.config.dto.request.common.DeleteBody;
import tianrun.ziguan.api.calculate.config.dto.request.common.add.edit.SaveBody;
import tianrun.ziguan.api.calculate.config.entity.TCalAsset;
import tianrun.ziguan.api.calculate.config.entity.TTemplate;
import tianrun.ziguan.api.calculate.config.listener.RefreshCacheListener;
import tianrun.ziguan.api.calculate.config.service.cal.asset.ITCalAssetService;
import tianrun.ziguan.api.calculate.config.service.common.edit.AbstractDealService;
import tianrun.ziguan.api.calculate.config.service.ge.TGeTableConfigService;
import tianrun.ziguan.api.calculate.config.service.template.ITTemplateService;
import tianrun.ziguan.api.calculate.config.utils.MapUtil;
import tianrun.ziguan.api.calculate.config.utils.StringUtil;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class CalAssetDealService extends AbstractDealService {

    private final ITCalAssetService calAssetService;
    private final ITTemplateService templateService;

    public CalAssetDealService(TGeTableConfigService tableConfigService, ITCalAssetService calAssetService, ITTemplateService templateService) {
        super(tableConfigService);

        this.calAssetService = calAssetService;
        this.templateService = templateService;
    }

    @Override
    public void before(SaveBody body) {
        String assetNum = MapUtil.getString(body.getData(), "asset_num");
        if (StringUtil.isEmpty(assetNum)) return;
        LambdaQueryWrapper<TCalAsset> wrapper = Wrappers.<TCalAsset>lambdaQuery().
                eq(TCalAsset::getAssetNum, assetNum);
        if (Objects.equals(body.getOption(), SaveBody.Option.EDIT)) {
            wrapper.ne(TCalAsset::getId, body.getId());
            body.getRawData().put("old", calAssetService.getById(body.getId()));
        }
        int count = calAssetService.count(wrapper);
        Assert.isTrue(count == 0, assetNum + "已存在！");
    }


    @Override
    public String tableName() {
        return "t_cal_asset";
    }

    @Override
    public void after(SaveBody body) {
        TCalAsset old = MapUtil.getObject(body.getRawData(), "old", TCalAsset.class);
        String assetNum = MapUtil.getString(body.getData(), "asset_num");

        if (old != null) {
            List<TCalAsset> calAssets = calAssetService.queryChildAssets(old.getAssetNum());
            calAssets.forEach(asset -> asset.setParentAssetNum(assetNum));
            if (!calAssets.isEmpty())
                calAssetService.updateBatchById(calAssets);
        }
        RefreshCacheListener.flushByAssetId(body.getId());
    }

    @Override
    public void delBefore(DeleteBody body) {

    }

    @Override
    public void listAfter(List<Map<String, Object>> result) {

    }

    @Override
    public void delAfter(DeleteBody body) {
        body.getIds().forEach(RefreshCacheListener::flushByAssetId);
    }

    @Override
    public void infoAfter(Map<String, Object> bean) {
        Long templateId = MapUtil.getLong(bean, "template_id");
        String templateName;
        if (Objects.isNull(templateId) || Objects.equals(templateId, 0L)) {
            templateName = "无模板";
        } else {
            TTemplate tTemplate = templateService.getById(templateId);
            if (tTemplate == null) {
                templateName = "无模板";
            } else {
                templateName = tTemplate.getName();
            }
        }
        bean.put("template_name", templateName);
    }


}
